package org.campuscontent.model;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;

@SuppressWarnings( {"deprecation", "unused"} )
public class AllocTableRow extends JPanel implements ActionListener, FocusListener, MouseListener
{

    JLabel conLabel = new JLabel();
    AllocTable alloc;
    JButton delete;
    JTextField[] textField = new JTextField[ 3 ];
    String indexStr = "";

    public AllocTableRow(
                          AllocTable alloc,
                          String text )
    {
        ImageIcon icon = new ImageIcon( this.getClass().getResource( "/org/campuscontent/images/toolbar/delete.gif" ) );
        this.alloc = alloc;
        this.setLayout( new FlowLayout( FlowLayout.LEFT, 5, 2 ) );
        this.addMouseListener( this );

        boolean emptyExist = false;
        int len = text.length();
        if ( (text.substring( len - 2, len - 1 )).equals( "," ) ) emptyExist = true;
        String con = text.replaceAll( "\\),", "," );
        con = con.substring( 2, con.length() - 1 );
        String[] connection = con.split( "," );

        if ( emptyExist )
        {
            String strTemp[] = { connection[ 0 ], connection[ 1 ], "" };
            connection = strTemp;
        }
        JTextField[] textField = new JTextField[ 3 ];
        String[] tooltips = { Config.TooltipSource, Config.TooltipEntry, Config.TooltipEnd };
        JTextField textfe = this.pseudoLabel( 15, text, alloc.getBackground() );
        // textfe.setMargin(new Insets(0,0,0,0));
        textfe.addMouseListener( this );
        addComponent( this, textfe, "" );
        for ( int i = 0; i < textField.length; i++ )
        {
            textField[ i ] = textfield( 10, connection[ i ], Color.WHITE );
            textField[ i ].setActionCommand( "1" );
            textField[ i ].setToolTipText( tooltips[ i ] );
            textField[ i ].addActionListener( this );
            textField[ i ].addFocusListener( this );
            textField[ i ].addMouseListener( this );
            // textField[i].setMargin(new Insets(0,0,0,0));
            addComponent( this, textField[ i ], "" );
        }
        delete = new JButton();
        delete.setBorderPainted( false );
        delete.setActionCommand( "4" );
        delete.addActionListener( this );
        delete.setFocusPainted( false );
        delete.addMouseListener( this );
        delete.setIcon( icon );
        delete.setMargin( new Insets( 0, 0, 0, 0 ) );
        this.add( delete );
    }

    public JTextField getFirstColumnTextField()
    {
        JTextField text = null;
        Component comp = this.getComponent( 0 );
        if ( comp instanceof JTextField )
        {
            text = (JTextField) comp;
        }
        return text;
    }

    public JTextField getColumnTextField( int column )
    {
        JTextField text = null;
        if ( column < this.countComponents() )
        {
            Component comp = this.getComponent( column );
            if ( comp instanceof JTextField )
            {
                text = (JTextField) comp;
            }
        }
        return text;
    }

    public JTextField pseudoLabel( int cols, String text, Color background )
    {
        JTextField textfield = new JTextField( cols );
        textfield.setEditable( false );
        textfield.setBorder( null );
        textfield.setBackground( background );
        textfield.setText( text );
        // textfield.setFont(Fonts.GUIActualFont);
        return textfield;
    }

    public JTextField textfield( int cols, String text, Color background )
    {
        JTextField textfield = new JTextField( cols );
        textfield.setBackground( background );
        textfield.setText( text );
        // textfield.setFont(Fonts.GUIActualFont);
        return textfield;
    }

    public void focusGained( FocusEvent e )
    {
        indexStr = this.getColumnTextField( 0 ).getText().trim();
    }

    public void focusLost( FocusEvent e )
    {
        Component comp = e.getComponent();
        if ( comp instanceof JTextField )
        {
        }

        String content1 = this.getColumnTextField( 1 ).getText().trim();
        String content2 = this.getColumnTextField( 2 ).getText().trim();
        String content3 = this.getColumnTextField( 3 ).getText().trim();
        String[] str = { content1, content2, content3 };

        String rowStr = alloc.inputRowToString( str );
        this.getColumnTextField( 0 ).setText( rowStr );

        int indexRow = alloc.getRowIndex( indexStr );
        if ( indexRow == -1 )
        {
        }
        else alloc.replaceRow( indexRow, rowStr );
        alloc.mf.templateBase.allocScrollPane.validate();
//        System.out.println( alloc.allRows );

    }

    public void mouseClicked( MouseEvent e )
    {
    }

    public void mousePressed( MouseEvent e )
    {
    }

    public void mouseReleased( MouseEvent e )
    {
    }

    public void mouseEntered( MouseEvent e )
    {
        MainFrame mf = alloc.getMainFrame();

    }

    public void mouseExited( MouseEvent e )
    {
        MainFrame mf = alloc.getMainFrame();

    }

    // Close the dialog on a button event
    public void actionPerformed( ActionEvent event )
    {

        int command = (int) (Double.valueOf( event.getActionCommand() ).doubleValue());
        String content1 = this.getColumnTextField( 1 ).getText();
        String content2 = this.getColumnTextField( 2 ).getText();
        String content3 = this.getColumnTextField( 3 ).getText();
        String[] str = { content1, content2, content3 };
        switch ( command )
        {
            case 1:
                alloc.getMainFrame().templateBase.allocScrollPane.validate();
            break;
            case 4:
                String rowStr = alloc.inputRowToString( str );
                int indexRow = alloc.getRowIndex( rowStr );
                if ( indexRow == -1 )
                {
                }
                else alloc.deleteRow( indexRow );
                // System.out.println(content1+content2+content3);
                alloc.getMainFrame().templateBase.allocScrollPane.validate();
            break;
        }

    }

    public void addComponent( JPanel parent, Component child, String constraints )
    {
        if ( constraints.equals( "" ) )
        {
            parent.add( child );
        }
        else
        {
            parent.add( child, constraints );
        }
    }

}
